//
//  weekSelectController.m
//  QRMaster
//
//  Created by myApple on 15/7/21.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "weekSelectController.h"
#import "PromptInfoView.h"
#import "BackBtn.h"

@interface weekSelectController ()<UITableViewDataSource,UITableViewDelegate,UINavigationControllerDelegate>
{
    NSArray * weakData;
    UITableView * weakTableView;
    NSMutableArray * selectArray;//后台交互的数据
    NSMutableArray * prarmArray;//显示的数据
}

@end

@implementation weekSelectController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        selectArray = [[NSMutableArray alloc]init];
        prarmArray = [[NSMutableArray alloc]init];
        weakData = @[NSLocalizedString(@"Sunday", nil),NSLocalizedString(@"Monday", nil),NSLocalizedString(@"Tuesday", nil),NSLocalizedString(@"Wednesday", nil),NSLocalizedString(@"Thursday", nil),NSLocalizedString(@"Friday", nil),NSLocalizedString(@"Saturday", nil)];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
     // Do any additional setup after loading the view.
    self.title = NSLocalizedString(@"weekSelect", nil);
    [self prepareTableView];
//    UIButton * btn = [BackBtn initWithFrame:CGRectMake(16, 0, 40, 40) withImage:@"navigationBack"];
//    [btn addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
//     self.navigationController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    UIButton * backBtn = [BackBtn initWithFrame:CGRectMake(0, 0, 40, 40) withImage:@"navigationBack"];
    [backBtn addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftitem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftitem;
}

-(void)setSelectedDay:(NSString *)selectedDay
{
    _selectedDay = selectedDay;
    if (selectedDay == nil) {
        selectArray = [[NSMutableArray alloc]initWithArray:@[@"0",@"1",@"2",@"3",@"4",@"5",@"6"]];
        prarmArray = [weakData mutableCopy];
    }
    else
    {
        selectArray = [[selectedDay componentsSeparatedByString:@","] mutableCopy];
        for (int i = 0 ; i < selectArray.count; i ++) {
            [prarmArray addObject:weakData[[selectArray[i] integerValue]]];
        }
    }
    
}

-(void)prepareTableView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    weakTableView = [[UITableView alloc]initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStyleGrouped];
    weakTableView.dataSource = self;
    weakTableView.delegate = self;
    [self.view addSubview:weakTableView];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return weakData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.textLabel.text = weakData[indexPath.row];
    if (indexPath.row == 0 || indexPath.row == 6) {
        cell.textLabel.textColor = [UIColor redColor];
    }
    if (selectArray.count > 0) {
        for (NSString * string in selectArray) {
            if ([string isEqualToString:[NSString stringWithFormat:@"%d",(int)indexPath.row]]) {
                 cell.accessoryType = UITableViewCellAccessoryCheckmark;
                return cell;
            }
        }
            cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [selectArray removeObject:[NSString stringWithFormat:@"%d",(int)indexPath.row]];
        [prarmArray removeObject:cell.textLabel.text];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [selectArray addObject:[NSString stringWithFormat:@"%d",(int)indexPath.row]];
        [prarmArray addObject:cell.textLabel.text];
    }
}

-(void)popBack
{
    if (_getDays) {
        _getDays(selectArray,prarmArray);
    }
    if(selectArray.count < 1)
    {
        [PromptInfoView showMessage:NSLocalizedString(@"noWeekSelect", nil)superView:self.view];
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
